<?php
@include_once "Crypt3Des.php";
class seat {
   // const URL = "http://api.dsjtour.com/api/openinterface.ashx?account=hqf@op3.com";
    const URL = "http://api2.0368.com/api/openinterface.ashx?account=hqf@op3.com";

    const ACCOUNT = 'hqf@op3.com';
    const ACCESSKEY = '03447b85a97c4631b1fc83a4445c84f9';
    const KEY = '0766169714b5497b9748c821';
    const ORGANIZATION = '83';

    public static function getCrypt(){
        $Crypt3Des = new Crypt3Des(self::KEY);
        return $Crypt3Des;
    }

    /**
 * post请求
 */
    public static function request_post($url, $param,$is_format=true) {
        if(empty($url)){
            $url = self::URL;
        }
        $postUrl = $url;
        if($is_format){
            $curlPost =self::requestFormat($param);
        } else {
            $curlPost =$param;
        }
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch);//运行curl
        if($error = curl_error($ch)) {
            curl_close($ch);
            return $error;
        }else{
            curl_close($ch);
        }
        return self::responseFormat($data);
    }


    /**
     * 请求数据格式化
     */
    public static function requestFormat($data){
        $postData = json_encode($data); //json 加密
        $postData =  self::getCrypt()->encrypt($postData); //3des加密
        $encode = mb_detect_encoding ( $postData );
        if ($encode != "UTF-8") {
            $postData = iconv ( $encode, "UTF-8", $postData );
        }
        return $postData;
    }

    /**
     * 处理响应数据
     */
    public static function responseFormat($data){
        $responseData = self::getCrypt()->decrypt($data);
        $response = str_replace ( array (
            "\r\n",
            "\r",
            "\n"
        ), "", $responseData );
        $response = json_decode ( $response, true );
        return $response;
    }

    /**
     * 头信息
     */
    public static  function getHeader($method,$organization){

        $organization = !empty($organization) ? $organization : self::ORGANIZATION;
        $sequenceId = strval ( mt_rand ( 10000000, 99999999 ) ); // 8位随机数;
        $timeStamp = date ( 'YmdHis' ); // 年月日时分秒
        $accesskey = self::ACCESSKEY;
        $sign = strtoupper( md5($organization . $sequenceId . $timeStamp . $accesskey) );
        $header = array (
            'sequenceid' => $sequenceId,
            'sign' => $sign,
            'timestamp' => $timeStamp,
            'organization' => $organization,
            'account' => self::ACCOUNT,
            'version' => '2.0',
            'method' =>$method
        );
        return $header;
    }

    /**
     * 构建表单
     */
    public static function buildForm($formUrl,$param,$button){
        $html = '<form action="'.$formUrl.'" method="post" id="buildform" style="display:none;" name="buildform"  >';
            foreach($param as $key => $val){
                $html.="<input type='hidden' name='".$key."' id='".$key."' value='".$val."'  />";
            }
        $html = $html ."<input type='submit' value='".$button."'  /></form>";
        $html = $html."<script>document.forms['buildform'].submit();</script>";

        return $html;
    }









}



?>